package com.putong35.common.app.data;

import android.util.Log;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.putong35.common.app.BaseApplication;
import com.putong35.common.httprequest.OnFailureListener;
import com.putong35.common.httprequest.OnSuccessListener;
import com.putong35.common.httprequest.retrofit.HttpCommonCallback;
import com.putong35.common.httprequest.retrofit.ResponseData;
import com.putong35.common.httprequest.retrofit.converter.GsonResponseBodyConverter;
import com.putong35.common.httprequest.retrofit.converter.HttpError;

import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;

public class HttpCommonApiHelper {
    public static final String TAG = HttpCommonApiHelper.class.getSimpleName();


    /**
     * 获取短信验证码
     * @param type
     * @param mobile
     * @param successListener
     * @param failureListener
     */
    public static void getSmsCode(String type, String mobile, OnSuccessListener<String> successListener, OnFailureListener failureListener) {
        Retrofit retrofit = BaseApplication.retrofit();
        HttpCommonApi request = retrofit.create(HttpCommonApi.class);
        Call<String> call = request.getSmsCode(type, mobile);
        HttpCommonCallback<String> callback = new HttpCommonCallback<>(successListener, failureListener);
        call.enqueue(callback);
    }


    public static <Bean> void httpGet(String url, HashMap<String, String> mapParams, Class<Bean> clazz, OnSuccessListener<Bean> successListener, OnFailureListener failureListener) {
        Retrofit retrofit = BaseApplication.retrofit();
        HttpCommonApi request = retrofit.create(HttpCommonApi.class);
        if (mapParams == null) {
            mapParams = new HashMap<>();
        }

        Call<ResponseBody> call = request.httpGet(url, mapParams);
        call.enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                Log.d(TAG, "onResponse: ");
                try {
                    Type type = new TypeToken<Bean>() {
                    }.getType();
                    GsonResponseBodyConverter<Bean> converter = new GsonResponseBodyConverter<Bean>(new Gson(), type, clazz);
                    Bean beanData = converter.convert(response.body());
                    if (beanData != null) {
                        if (successListener != null) {
                            successListener.onSuccess(1, "成功", beanData, null);
                        }

                    } else {
                        if (failureListener != null) {
                            // TODO 具体的服务器错误code
                            failureListener.onFailure(-1, "数据为空");
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    onFailure(null, e);
                }
            }

            @Override
            public void onFailure(Call<ResponseBody> call, Throwable throwable) {
                throwable.printStackTrace();
                int code = -1;
                if (throwable instanceof HttpError) {
                    HttpError error = ((HttpError) throwable);
                    if (error.body instanceof ResponseData) {
                        ResponseData data = ((ResponseData) error.body);
                        code = data.code;
                    }

                }
                Log.d(TAG, "onFailure: " + throwable.getMessage());
                if (failureListener != null) {
                    failureListener.onFailure(code, throwable.getMessage());
                }
            }
        });
    }



    public static <Bean> void httpGetList(String url, HashMap<String, String> mapParams, Class<Bean> clazz, OnSuccessListener<List<Bean>> successListener, OnFailureListener failureListener) {
        Retrofit retrofit = BaseApplication.retrofit();
        HttpCommonApi request = retrofit.create(HttpCommonApi.class);
        if (mapParams == null) {
            mapParams = new HashMap<>();
        }
        //TODO make params

       /* HashMap<String, String> map = new HashMap<>();
        map.put("appVersion", "1.0");
        map.put("content", content);
        map.put("type", ""+type);*/

        Call<ResponseBody> call = request.httpGetList(url, mapParams);
        HttpCommonCallback<List<Bean>> callback = new HttpCommonCallback<>(successListener, failureListener);
        call.enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                Log.d(TAG, "onResponse: ");
                try {
                    Type type = new TypeToken<Bean>() {
                    }.getType();
                    GsonResponseBodyConverter<Bean> converter = new GsonResponseBodyConverter<Bean>(new Gson(), type, clazz);
                    List<Bean> list = converter.convertList(response.body());
                    if (list != null) {
                        if (successListener != null) {
                            successListener.onSuccess(1, "成功", list, null);
                        }

                    } else {
                        if (failureListener != null) {
                            // TODO 具体的服务器错误code
                            failureListener.onFailure(-1, "数据为空");
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    onFailure(null, e);
                }
            }

            @Override
            public void onFailure(Call<ResponseBody> call, Throwable throwable) {
                throwable.printStackTrace();
                int code = -1;
                if (throwable instanceof HttpError) {
                    HttpError error = ((HttpError) throwable);
                    if (error.body instanceof ResponseData) {
                        ResponseData data = ((ResponseData) error.body);
                        code = data.code;
                    }

                }
                Log.d(TAG, "onFailure: " + throwable.getMessage());
                if (failureListener != null) {
                    failureListener.onFailure(code, throwable.getMessage());
                }
            }
        });
    }

}
